package com.kawins.util.colle;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author rankai
 *         createTime 2017-04-2017/4/28 10:32
 */
public class MapKit {

    /**
     * 移除map中空字段
     *
     * @param map 参数map
     * @return 没有空字段的map
     */
    public static Map<String, Object> removeNullForObjByMap(Map<String, Object> map) {
        if (map.isEmpty()) {
            return map;
        }
        Map<String, Object> newMap = new HashMap();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (StringUtils.isBlank(entry.getKey()) || StringUtils.isBlank(String.valueOf(entry.getValue()))) {
                continue;
            }
            newMap.put(entry.getKey(), entry.getValue());
        }
        return newMap;
    }

    /**
     * 移除map中空字段
     *
     * @param map 参数map
     * @return 没有空字段的map
     */
    public static Map<String, String> removeNullForStrByMap(Map<String, String> map) {
        if (map.isEmpty()) {
            return map;
        }
        Map<String, String> newMap = new HashMap();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (StringUtils.isBlank(entry.getKey()) || StringUtils.isBlank(entry.getValue())) {
                continue;
            }
            newMap.put(entry.getKey(), entry.getValue());
        }
        return newMap;
    }

    /**
     * 移除list中空字段
     *
     * @param list 参数list
     * @return 没有空字段的list
     */
    public static List<Object> removeNullForObjByList(List<Object> list) {
        if (list.isEmpty()) {
            return list;
        }
        List<Object> newList = new ArrayList<>();
        for (Object object : list) {
            if (StringUtils.isNotBlank(String.valueOf(object))) {
                newList.add(object);
            }
        }
        return newList;
    }

    /**
     * 移除list中空字段
     *
     * @param list 参数list
     * @return 没有空字段的list
     */
    public static List removeNullForStrByList(List<String> list) {
        if (list.isEmpty()) {
            return list;
        }
        List<String> newList = new ArrayList<>();
        for (String string : list) {
            if (StringUtils.isNotBlank(string)) {
                newList.add(string);
            }
        }
        return newList;
    }

    /**
     * 创建
     *
     * @param <T>
     * @param <V>
     */
    static class HashMap<T, V> extends java.util.HashMap<T, V> {
        public static <T, V> HashMap build() {
            return new HashMap<T, V>();
        }

        public HashMap<T, V> puts(T key, V value) {
            super.put(key, value);
            return this;
        }
    }

    /**
     * 排序
     */
    public static class MapSort {

        /**
         * 移除map中空字段
         *
         * @param map 参数map
         * @return 没有空字段的map
         */
        public static Map<String, Object> removeNullForObjByMap(Map<String, Object> map) {
            if (map.isEmpty()) {
                return map;
            }
            Map<String, Object> newMap = new java.util.HashMap();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                if (StringUtils.isBlank(entry.getKey()) || StringUtils.isBlank(String.valueOf(entry.getValue()))) {
                    continue;
                }
                newMap.put(entry.getKey(), entry.getValue());
            }
            return newMap;
        }

        /**
         * 移除map中空字段
         *
         * @param map 参数map
         * @return 没有空字段的map
         */
        public static Map<String, String> removeNullForStrByMap(Map<String, String> map) {
            if (map.isEmpty()) {
                return map;
            }
            Map<String, String> newMap = new java.util.HashMap();
            for (Map.Entry<String, String> entry : map.entrySet()) {
                if (StringUtils.isBlank(entry.getKey()) || StringUtils.isBlank(entry.getValue())) {
                    continue;
                }
                newMap.put(entry.getKey(), entry.getValue());
            }
            return newMap;
        }

        /**
         * 移除list中空字段
         *
         * @param list 参数list
         * @return 没有空字段的list
         */
        public static List<Object> removeNullForObjByList(List<Object> list) {
            if (list.isEmpty()) {
                return list;
            }
            List<Object> newList = new ArrayList<>();
            for (Object object : list) {
                if (StringUtils.isNotBlank(String.valueOf(object))) {
                    newList.add(object);
                }
            }
            return newList;
        }

        /**
         * 移除list中空字段
         *
         * @param list 参数list
         * @return 没有空字段的list
         */
        public static List removeNullForStrByList(List<String> list) {
            if (list.isEmpty()) {
                return list;
            }
            List<String> newList = new ArrayList<>();
            for (String string : list) {
                if (StringUtils.isNotBlank(string)) {
                    newList.add(string);
                }
            }
            return newList;
        }
    }
}
